package com.aoxin.service.impl.activity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.activity.RecommendMoneyMapper;
import com.aoxin.mapper.activity.SlfCashMapper;
import com.aoxin.mapper.opm.SlfUserMapper;
import com.aoxin.model.activity.SlfCash;
import com.aoxin.model.activity.SlfRecommendMoney;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.service.inter.activity.RecommendMoneyService;
import com.aoxin.service.inter.activity.SlfCashService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.IsWithdraw;

/**
 * 提现历史impl
 * 
 * @author lop
 * @date 2018年5月7日 下午2:31:35
 */
@Service("slfCashServiceImpl")
public class SlfCashServiceImpl implements SlfCashService {

	@Autowired
	private SlfCashMapper slfcashmapper;
	@Autowired
	RecommendMoneyMapper recommendMoneyMapper;
	@Autowired
	private RecommendMoneyService recommendMoneyService;
	 @Autowired
	 private SlfUserMapper slfUserMapper;
	 
	 
	@Override
	public List<SlfCash> getSlfCash(SlfCash slfcash) {
		return this.slfcashmapper.getSlfCash(slfcash);
	}

	@Override
	@Transactional
	public void insertSelectives(Integer type) {
		/***
		 *  type 提现类型
		 *    之前业务逻辑用户一个个提现，现在直接全部提现。
		 *    直接写单条数据   并且更新 slf_user 表父父级是否获得提现状态
		 *    直接查询所有可提现数据  同时写入数据，并更新状态 （包括子集，子子集 ）
		 */
		Assert.notNull(type, String.format(ErrorUtil.PARAMETER_NULL, "type"));
		// 用于客户端首次点击提现标志
		SlfUser user = ThreadLocalUtil.getUser();
		if (null != user) {
			SlfRecommendMoney slfRecommendMoney = new SlfRecommendMoney();
			slfRecommendMoney.setRefereeId(user.getUserId());
			slfRecommendMoney.setIsWithdraw(IsWithdraw.GETRECOMMENDED.getId());
			// 获取到所有推荐表有金额数据  子级
			List<SlfRecommendMoney> slfRecommendMoneys = this.recommendMoneyService.getRecommendMoney(slfRecommendMoney);
			List<Integer> sonIdList=new ArrayList<Integer>();
			Integer  integer=this.recommendMoneyMapper.canWithdrawMoney(user.getUserId());
			StringBuilder strbud=new StringBuilder();
			// 更新slf_user 表子子级状态 	
			for(int a=0,b=slfRecommendMoneys.size();a<b;a++){
				SlfRecommendMoney slfRecommendMoney1=slfRecommendMoneys.get(a);
				SlfRecommendMoney sonSlfRecommendMoney = new SlfRecommendMoney(); 
				//通过子级ID 查询子子级ID
				sonSlfRecommendMoney.setRefereeId(slfRecommendMoney1.getReferralsId());
				//查询子级和孙子级对应关系
				List<SlfRecommendMoney> grandsonSlfRecommendMoneys = this.recommendMoneyService.getRecommendMoney(sonSlfRecommendMoney);
				// 更新子子级状态
				for (SlfRecommendMoney slfRecommendMoney2 : grandsonSlfRecommendMoneys) {
					 	SlfUser slfUser = new SlfUser();
	                    slfUser.setUserId(slfRecommendMoney2.getReferralsId());
	                    slfUser.setIsWinners(IsWithdraw.AUDITING.getId());
					  this.slfUserMapper.updateByPrimaryKeySelective(slfUser);
				}
				// 添加子级ID
				sonIdList.add(slfRecommendMoney1.getReferralsId());
				if(a==(b-1)){
					strbud.append(slfRecommendMoney1.getReferralsId());	
				}else{
					strbud.append(slfRecommendMoney1.getReferralsId()+",");	
				}
			}	
			// 往提现表写入数据  
			SlfCash slfCash = new SlfCash();
			slfCash.setCashDate(new Date());
			slfCash.setCashMoney(new BigDecimal(integer));
			slfCash.setCashType(type);
			slfCash.setUserId(user.getUserId());
			slfCash.setUserName(user.getName());
			slfCash.setState(IsWithdraw.AUDITING.getId());
			slfCash.setRecommendMoneyId(strbud.toString());
			//添加推荐表数据
			this.slfcashmapper.insertSelective(slfCash);
			// 更新推荐表数据
			this.recommendMoneyMapper.updateIsWithdraw(sonIdList, IsWithdraw.AUDITING.getId());
		}
	}

	@Override
	@Transactional
	public Integer updateSlfCash(List<Integer> recommendMoneyId, Integer state) {
		
		/**
		 * 业务有误暂时未写
		 */
		//
		Assert.notNull(recommendMoneyId, String.format(ErrorUtil.PARAMETER_NULL, "recommendMoneyId"));
		return 0;
	}

	@Override
	@Transactional
	public void insertSelective(SlfCash slfCash) {
		//TODO 业务有误
		Assert.notNull(slfCash, String.format(ErrorUtil.PARAMETER_NULL, "slfCash"));
		SlfUser user = ThreadLocalUtil.getUser();
		if (null != user) {
			SlfRecommendMoney slfRecommendMoney = new SlfRecommendMoney();
			slfRecommendMoney.setRefereeId(user.getUserId());
		//	slfRecommendMoney.setRecommendMoneyId(slfCash.getRecommendMoneyId());
			slfRecommendMoney.setIsWithdraw(IsWithdraw.GETRECOMMENDED.getId());
			List<SlfRecommendMoney> slfRecommendMoneys = this.recommendMoneyService.getRecommendMoney(slfRecommendMoney);
			for (SlfRecommendMoney slfRecommendMoney2 : slfRecommendMoneys) {
				slfCash.setCashDate(new Date());
				slfCash.setCashMoney(slfRecommendMoney2.getRecommendMoney());
				slfCash.setUserId(user.getUserId());
				slfCash.setUserName(user.getName());
				slfCash.setState(IsWithdraw.AUDITING.getId());
			//	slfCash.setRecommendMoneyId(slfRecommendMoney2.getRecommendMoneyId());
				slfCash.setReferralsId(slfRecommendMoney2.getReferralsId());
				slfCash.setReferralsName(slfRecommendMoney2.getReferralsName());
				this.slfcashmapper.insertSelective(slfCash);
			}
		}
	}

}
